"""
css selector:css选择器 用其他方式无法定位的可以选择参数设定位
        标签选择器   input   h1 a div
        ID选择器
        标签+ID
        类选择器
        标签+类    input.s_ipt
        属性选择器   [][][]
        标签+属性   input[]
    模糊匹配    类正则表达：^以……开头；$以……结尾；*包含……
        [属性1^=‘值’]
        [属性1$=‘值’]
        [属性1*=‘值’]
    子元素选择器  父元素子元素的层级关系
    兄弟选择器
    后代选择器

"""
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
#定位百度输入框
#<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("A")
driver.find_element(By.CSS_SELECTOR,"input#kw").send_keys("B")
driver.find_element(By.CSS_SELECTOR,'input[autocomplete="off"]').send_keys("C")
driver.find_element(By.CSS_SELECTOR,'input.s_ipt').send_keys("D")
driver.find_element(By.CSS_SELECTOR,'[maxlength="255"][autocomplete="off"]').send_keys("E")
driver.find_element(By.CSS_SELECTOR,'[class="s_ipt"][name="wd"]').send_keys("F")
sleep(3)
driver.find_element(By.CSS_SELECTOR,'[autocomplete^="of"]').send_keys("chenggong")
driver.find_element(By.CSS_SELECTOR,'[autocomplete$="f"]').send_keys("G")
driver.find_element(By.CSS_SELECTOR,'[autocomplete*="ff"]').send_keys("G")
sleep(2)

#子元素选择器
##s-top-left > a:nth-child(3)
driver.get("http://www.baidu.com")
#第一个孩子节点
a = driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(1)")
print(a.get_attribute("href"))
print(a.text)
a = driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:first-child")
print(a.get_attribute("href"))
print(a.text)
#第二个孩子节点
a = driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(2)")
print(a.get_attribute("href"))
print(a.text)
#最后一个孩子节点
a = driver.find_element(By.CSS_SELECTOR,"#s-top-left > div:last-child > a")
print(a.get_attribute("href"))
print(a.text)
sleep(3)
